﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yipusoft.MapInfo.BLL;

public partial class Map_SetMap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable tbl = GetMarkTypeTable();
            listType.DataSource = tbl;
            listType.DataValueField = "value";
            listType.DataTextField = "desc";
            listType.DataBind();
            string script = "<script language=javascript> function getExample() {";
            script += "var examples = new Array(" + tbl.Rows.Count.ToString() + ");";
            for (int i = 1; i < tbl.Rows.Count; i++)
            {
                script += "examples[" + tbl.Rows[i]["value"] + "]=\"" + tbl.Rows[i]["example"] + "\";";
            }
            script += "document.getElementById(\"ctl00_ContentPlaceHolder1_lblExample\").innerHTML=";
            script += "examples[document.getElementById(\"ctl00_ContentPlaceHolder1_listType\").value]; }";
            script += "</script>";
            Session["ExampleScript"] = script;
        }
        
        string javascript = Session["ExampleScript"] as string;
        if (javascript != null)
        {
            
            ClientScript.RegisterStartupScript(this.GetType(), "getExample", javascript);
            listType.Attributes.Add("onChange", "getExample()");
        }

        // Retrieve x and y where the mouse click took place
        string x = Request.Params["ctl00_ContentPlaceHolder1_imbMap.x"];
        string y = Request.Params["ctl00_ContentPlaceHolder1_imbMap.y"];
        // Get type of annotation selected
        string type = listType.SelectedValue;
        // Get any applicable argument
        string param = txtParam.Text;
        // Create the MarkableType
        MarkableType mark = MarkManager.CreateByID(type);
        if (mark != null && param.Length > 0)
        {
            mark.LoadFromString(param);
        }
        // Construct the new ImageURL
        if (x != null && y != null && x.Length >= 0 && y.Length >= 0 && mark != null)
        {
            string append = string.Join(",", new string[] { x, y, type, mark.SaveAsString() });
            imbMap.ImageUrl += HttpUtility.HtmlEncode(append + ";");
        }
    }
    private DataTable GetMarkTypeTable()
    {
        DataTable tbl = new DataTable();

        DataColumn col = new DataColumn("value", Type.GetType("System.String"));
        tbl.Columns.Add(col);

        col = new DataColumn("desc", Type.GetType("System.String"));
        tbl.Columns.Add(col);

        col = new DataColumn("example", Type.GetType("System.String"));
        tbl.Columns.Add(col);

        DataRow row = tbl.NewRow();
        row["value"] = "-1";
        row["desc"] = "";
        row["example"] = "";
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row["value"] = "3";
        row["desc"] = "Rectangle";
        row["example"] = "format: 'width,height'. eg. 5,6";
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row["value"] = "1";
        row["desc"] = "Circle";
        row["example"] = "format: 'radius'. eg. 10";
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row["value"] = "2";
        row["desc"] = "Text";
        row["example"] = "format: 'text'.";
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row["value"] = "3";
        row["desc"] = "Image";
        row["example"] = "format: 'image.jpg'. Type eg. left.ico, right.ico, stop.ico. Relative to the current directory";
        tbl.Rows.Add(row);

        tbl.AcceptChanges();

        return tbl;
    }
    protected void ddlMaps_SelectedIndexChanged(object sender, EventArgs e)
    {
        imbMap.ImageUrl =  "MarkUp.aspx?img="+ddlMaps.SelectedValue+"&desc=";
    }
}
